package com.ybb.order.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ybb.order.entity.OrderEnity;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper extends BaseMapper<OrderEnity> {

    @Update("update ybb_order set status=#{status} and pay_time=now() where id=#{id}")
    void updateOrderStatus(@Param("id") Integer id, @Param("status") Integer status);

    @Select("select id from ybb_order where status=#{status} and create_time<#{watermark}")
    List<Integer> getTimeOutOrders(@Param("watermark") Date watermark, @Param("status") Integer status);

    /**
     * 查询[start, end)时间段内支付的订单
     * @return 商户：金额
     */
    @Select("select merchant,sum(num*price) as amount from ybb_order where status=#{status} pay_time>=#{start} and pay_time<#{end} group by merchant")
    @MapKey("merchant")
    Map<String,Double> getOrdersAmount(@Param("start") Date start, @Param("end") Date end, @Param("status") Integer status);
}
